using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Ent.Log
{
    public enum Logtype { INFO = 1, TRACE = 3, ERROR = 7, ALL = 15 }
    public class Logger
    {
        static string _path;
        static string _format;
        static string codeText = DateTime.Now.ToString().Replace("/", "").Replace(":", "").Replace("\\", "").Replace(" ", "").Replace("P", "").Replace("A", "").Replace("M", "");
        static long code = Convert.ToInt64(codeText);
        UnicodeEncoding uniEncoding = new UnicodeEncoding();
        Logtype _logType = Logtype.ALL;
        static Logger _log = null;
        Logger(string path, string format, Logtype type)
        {
            _path = path;
            Write("**********************************************");
            Write("Logger started : " + DateTime.Now.ToString());
            Write("**********************************************");
            if (format == "default")
                _format = "[[LOGTYPE]] : [[DATE]] [[TIME]] : [[MESSAGE]]";
            _logType = type;
        }
        public static Logger GetLogger(string path, string format, Logtype type)
        {
            if (_log == null)
            {
                _log = new Logger(path, format, type);
            }
            return _log;
        }
        public void Info(string message)
        {
            if ((_logType & Logtype.INFO) == _logType)
            {
                string formatedMessage = DoFormat("INFO", message);
                Write(formatedMessage);
            }
        }
        public void Trace(string message)
        {
            if ((_logType & Logtype.TRACE) == _logType)
            {
                string formatedMessage = DoFormat("TRACE", message);
                Write(formatedMessage);
            }
        }
        public long Error(Exception exc)
        {
            string message = exc.Message + "\r\n" + exc.StackTrace;
            return Error(message);
        }
        public long Error(string message)
        {
            if ((_logType & Logtype.ERROR) == _logType)
            {
                string formatedMessage = DoFormat("ERROR", "[" + ++code + "] " + message);
                Write(formatedMessage);
            }
            return code;
        }
        private void Write(string formatedMessage)
        {
            File.AppendAllText(_path, "\r\n" + formatedMessage, Encoding.UTF8);
        }
        private string DoFormat(string logtype, string message)
        { 
            string retValue="";
            if (_format.Contains("[[LOGTYPE]]"))
                retValue = _format.Replace("[[LOGTYPE]]", logtype);
            if (_format.Contains("[[DATE]]"))
                retValue = retValue.Replace("[[DATE]]", DateTime.Now.ToLongDateString());
            if (_format.Contains("[[TIME]]"))
                retValue = retValue.Replace("[[TIME]]", DateTime.Now.ToLongTimeString());
            if (_format.Contains("[[MESSAGE]]"))
                retValue = retValue.Replace("[[MESSAGE]]", message);
            return retValue;
        }
    }
}
